<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/11/27
 * Time: 14:01
 */
namespace app\mec\controller;
use think\Db;
class Agent extends Base {

    public function index(){


        if(request()->isAjax ()){
            $params = input('param.');
            $where2 = [];
            //查询属于自己的市ID
            $info = Db::table('up_dl')->where('dl_id',session('uid1'))->find();
            if($info['dl_level'] == 1){
                $shis = Db::table('up_area')
                    ->where('level',2)
                    ->where('pid',$info['dl_sheng'])
                    ->column('district_id');
                $qus = Db::table('up_area')
                    ->where('level',3)
                    ->where('pid','in',$shis)
                    ->column('district_id');
                $dl_id_shi = Db::table('up_dl')
                    ->where('dl_shi','in',$shis)
                    ->where('dl_level',2)
                    ->where('dl_status',2)
                    ->column('dl_id');
                $dl_id_qu = Db::table('up_dl')
                    ->where('dl_qu','in',$qus)
                    ->where('dl_level',3)
                    ->where('dl_status',2)
                    ->column('dl_id');
                $id = array_merge($dl_id_shi,$dl_id_qu);
                if(empty($id)){
                    $id = '0';
                }
                $where2['dl_id'] = array('in',$id);
            }else{
                $qus = Db::table('up_area')
                    ->where('level',3)
                    ->where('pid','in',$info['dl_shi'])
                    ->column('district_id');
                $id = Db::table('up_dl')
                    ->where('dl_qu','in',$qus)
                    ->where('dl_level',3)
                    ->where('dl_status',2)
                    ->column('dl_id');
                if(empty($id)){
                    $id = '0';
                }
                $where2['dl_id'] = array('in',$id);
            }

            if(isset($params['key']) && $params['key'] != '')
            {
                $where2['dl_phone|dl_id'] = ['like', "%" . $params['key'] . "%"];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('dl')->where($where2)->count();// 获取总条数
            $lists = Db::name('dl')
                ->where($where2)
                ->page($Nowpage,$limits)
                ->order('dl_id desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                $vo['num'] = count(array_filter(explode(',',$vo['dl_num'])));
                if($vo['dl_level']==2){
                    $district = Db::table('up_area')->where('district_id',$vo['dl_shi'])->value('district');
                    $district1 = Db::table('up_area')->where('district_id',$vo['dl_qu'])->value('district');
                    $vo['address'] = $district.$district1;
                }elseif ($vo['dl_level']==3){
                    $district1 = Db::table('up_area')->where('district_id',$vo['dl_qu'])->value('district');
                    $vo['address'] = $district1;
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?dl_id')){
            session('dl_id',null);
        }
        return $this->fetch();
    }
    //账户信息

    public function account(){
        if(request()->isAjax()){
            $id = input('param.id');
            $info = Db::table('up_dl')->where('dl_id',$id)->find();
            $info['num'] = count(array_filter(explode(',',$info['dl_num'])));
            if($info['dl_level']==2){
                $district = Db::table('up_area')->where('district_id',$info['dl_shi'])->value('district');
                $district1 = Db::table('up_area')->where('district_id',$info['dl_qu'])->value('district');
                $info['address'] = $district.$district1;
            }elseif ($info['dl_level']==3){
                $district1 = Db::table('up_area')->where('district_id',$info['dl_qu'])->value('district');
                $info['address'] = $district1;
            }

            $where['s_id'] = array('IN',$info['dl_num']);
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('shops')
                ->where($where)->count();// 获取总条数
            $lists = Db::name('shops')
                ->where($where)
                ->page($Nowpage,$limits)
                ->select();
            foreach ($lists as $key=>&$vo){
                $vo['dl_id'] = $info['dl_id'];
                $vo['dl_name'] = $info['dl_name'];
                $vo['dl_phone'] = $info['dl_phone'];
                $vo['dl_account'] = $info['dl_account'];
                $vo['num'] = $info['num'];
                $vo['dl_balance'] = $info['dl_balance'];
                $vo['dl_time'] = $info['dl_time'];
                $vo['address'] = $info['address'];
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?dl_id')){
            session('dl_id',input('id'));
        }
        return $this->fetch();
    }
}